#install.packages("tidyverse")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
data1 <- read.csv( file = "/Users/jimenacampos/Documents/cookie_cats.csv", sep = ";")

head(data1, 6)
##   userid version sum_gamerounds retention_1 retention_7
## 1    116 gate_30              3       FALSE       FALSE
## 2    337 gate_30             38        TRUE       FALSE
## 3    377 gate_40            165        TRUE       FALSE
## 4    483 gate_40              1       FALSE       FALSE
## 5    488 gate_40            179        TRUE        TRUE
## 6    540 gate_40            187        TRUE        TRUE
checkData <-
  print("##################### Shape #####################")
## [1] "##################### Shape #####################"
  print(dim(data1))
## [1] 90189     5
  print("##################### Types #####################")
## [1] "##################### Types #####################"
  print(str(data1))
## 'data.frame':    90189 obs. of  5 variables:
##  $ userid        : int  116 337 377 483 488 540 1066 1444 1574 1587 ...
##  $ version       : chr  "gate_30" "gate_30" "gate_40" "gate_40" ...
##  $ sum_gamerounds: int  3 38 165 1 179 187 0 2 108 153 ...
##  $ retention_1   : logi  FALSE TRUE TRUE FALSE TRUE TRUE ...
##  $ retention_7   : logi  FALSE FALSE FALSE FALSE TRUE TRUE ...
## NULL
  print("##################### Head #####################")
## [1] "##################### Head #####################"
  head(data1,3)
##   userid version sum_gamerounds retention_1 retention_7
## 1    116 gate_30              3       FALSE       FALSE
## 2    337 gate_30             38        TRUE       FALSE
## 3    377 gate_40            165        TRUE       FALSE
  print("##################### Tail #####################")
## [1] "##################### Tail #####################"
  tail(data1,3)
##        userid version sum_gamerounds retention_1 retention_7
## 90187 9999710 gate_30             28        TRUE       FALSE
## 90188 9999768 gate_40             51        TRUE       FALSE
## 90189 9999861 gate_40             16       FALSE       FALSE
  print("##################### NA #####################")
## [1] "##################### NA #####################"
  colSums(is.na(data1))
##         userid        version sum_gamerounds    retention_1    retention_7 
##              0              0              0              0              0
sum(duplicated(data1$userid))
## [1] 0
glimpse(data1)
## Rows: 90,189
## Columns: 5
## $ userid         <int> 116, 337, 377, 483, 488, 540, 1066, 1444, 1574, 1587, 1…
## $ version        <chr> "gate_30", "gate_30", "gate_40", "gate_40", "gate_40", …
## $ sum_gamerounds <int> 3, 38, 165, 1, 179, 187, 0, 2, 108, 153, 3, 0, 30, 39, …
## $ retention_1    <lgl> FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRU…
## $ retention_7    <lgl> FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, T…
# Número de jugadores en cada grupo AB

data1 %>%
    select(version) %>%
    group_by(version) %>%
    tally()
## # A tibble: 2 × 2
##   version     n
##   <chr>   <int>
## 1 gate_30 44700
## 2 gate_40 45489
#Contar el número de jugadores por cada ronda de juego

plot_df <- data1 %>%
    group_by(sum_gamerounds) %>%
    summarise(userid_cnt = n())

head(plot_df, 5)
## # A tibble: 5 × 2
##   sum_gamerounds userid_cnt
##            <int>      <int>
## 1              0       3994
## 2              1       5538
## 3              2       4606
## 4              3       3958
## 5              4       3629
# Cálculo de la retención de 1 día por cada grupo AB            
data1 %>%
    select(version, retention_1) %>%
    group_by(version) %>%
    summarise(return_pct = sum(retention_1)/n())
## # A tibble: 2 × 2
##   version return_pct
##   <chr>        <dbl>
## 1 gate_30      0.448
## 2 gate_40      0.442
# Cálculo de la retención de 7 días por cada grupo AB
data1 %>%
    select(version, retention_7) %>%
    group_by(version) %>%
    summarise(return_pct = sum(retention_7)/n())
## # A tibble: 2 × 2
##   version return_pct
##   <chr>        <dbl>
## 1 gate_30      0.190
## 2 gate_40      0.182
#install.packages("plotly")
#library(tidyverse)
glimpse(data1)
## Rows: 90,189
## Columns: 5
## $ userid         <int> 116, 337, 377, 483, 488, 540, 1066, 1444, 1574, 1587, 1…
## $ version        <chr> "gate_30", "gate_30", "gate_40", "gate_40", "gate_40", …
## $ sum_gamerounds <int> 3, 38, 165, 1, 179, 187, 0, 2, 108, 153, 3, 0, 30, 39, …
## $ retention_1    <lgl> FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRU…
## $ retention_7    <lgl> FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, T…
prop.table(table(data1$version))*100
## 
##  gate_30  gate_40 
## 49.56259 50.43741
colSums(is.na(data1))
##         userid        version sum_gamerounds    retention_1    retention_7 
##              0              0              0              0              0
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
fig <- data1 %>%
  plot_ly(
    x = ~version,
    y = ~sum_gamerounds,
    split = ~version,
    type = 'violin',
    box = list(
      visible = T
    ),
    meanline = list(
      visible = T
    )
  ) 

fig <- fig %>%
  layout(
    xaxis = list(
      title = "Gate"
    ),
    yaxis = list(
      title = "Sum gamerounds",
      zeroline = F
    )
  )

fig
cookie_cats_clean <- data1 %>% 
  filter(sum_gamerounds <= 40000)

library(plotly)

fig2 <- cookie_cats_clean %>%
  plot_ly(
    x = ~version,
    y = ~sum_gamerounds,
    split = ~version,
    type = 'violin',
    box = list(
      visible = T
    ),
    meanline = list(
      visible = T
    )
  ) 

fig2 <- fig2 %>%
  layout(
    xaxis = list(
      title = "Gate"
    ),
    yaxis = list(
      title = "Sum gamerounds",
      zeroline = F
    )
  )

fig2
number_of_games <- cookie_cats_clean %>%
  count(sum_gamerounds)

head(number_of_games, 6)
##   sum_gamerounds    n
## 1              0 3994
## 2              1 5538
## 3              2 4606
## 4              3 3958
## 5              4 3629
## 6              5 2992
#Hubo 3994 jugadores o el 4,4% del total de jugadores registrados que no jugaron ninguna ronda. Intentaremos ver la distribución de las 100 primeras rondas jugadas por cada usuario.
#install.packages("ggplot2")
#install.packages("dplyr")
#install.packages("plotly")
#install.packages("hrbrthemes")
library(ggplot2)
library(dplyr)
library(plotly)

p <- number_of_games %>%
  filter(sum_gamerounds <= 100) %>% 
  ggplot( aes(x=sum_gamerounds, y=n)) +
    geom_area(fill="#69b3a2", alpha=0.5) +
    geom_line(color="#69b3a2") +
  xlab("Number of rounds")+
    ylab("Number of Player") +
  ggtitle("Number of Player Played The First 100 Rounds")

# Turn it interactive with ggplotly
p <- ggplotly(p)
#A pesar de que el número de personas que jugaban más rondas estaba disminuyendo, podemos ver que había muchos jugadores que jugaban más rondas que el resto. Significa que este jugador está enganchado con el juego.
prop.table(table(cookie_cats_clean$retention_1))*100
## 
##    FALSE     TRUE 
## 55.47856 44.52144
#4.5% jugador vuelve después de instalar el juego por un día. El número es menor que la mayoría de los jugadores que decidieron no jugar después de 1 día. También podemos ver el número de retención de 1 día para cada grupo de puertas.
ratio_per_group1 <- cookie_cats_clean %>% 
  group_by(version, retention_1) %>% 
  summarize(count =n()) %>% 
  mutate(percentage = round(count/sum(count)*100,2)) %>% 
  ungroup() 
## `summarise()` has grouped output by 'version'. You can override using the
## `.groups` argument.
ratio_per_group1
## # A tibble: 4 × 4
##   version retention_1 count percentage
##   <chr>   <lgl>       <int>      <dbl>
## 1 gate_30 FALSE       24665       55.2
## 2 gate_30 TRUE        20034       44.8
## 3 gate_40 FALSE       25370       55.8
## 4 gate_40 TRUE        20119       44.2
#Para ambas puertas, el número de retención de 1 día es similar, alrededor del 44 %. También podemos ver la retención de 7 días. La retención de 7 días es una condición en la que un jugador vuelve a jugar después de 7 días de instalación. Veamos la proporción de todas las puertas.
prop.table(table(data1$retention_7))*100
## 
##    FALSE     TRUE 
## 81.39352 18.60648
#Los números de retención durante 7 días después de instalar el juego están bastante lejos. Muchos jugadores por la cantidad de 81% eligen abandonar el juego.
ratio_per_group7 <- data1 %>% 
  group_by(version, retention_7) %>% 
  summarize(count =n()) %>% 
  mutate(percentage = round(count/sum(count)*100,2)) %>% 
  ungroup() 
## `summarise()` has grouped output by 'version'. You can override using the
## `.groups` argument.
ratio_per_group7
## # A tibble: 4 × 4
##   version retention_7 count percentage
##   <chr>   <lgl>       <int>      <dbl>
## 1 gate_30 FALSE       36198       81.0
## 2 gate_30 TRUE         8502       19.0
## 3 gate_40 FALSE       37210       81.8
## 4 gate_40 TRUE         8279       18.2
#Para ambas puertas en retención de 7 días, el número es similar. 
cookie_cats_clean_30 <- cookie_cats_clean %>%
  dplyr::filter(version == "gate_30")

cookie_cats_clean_40 <- cookie_cats_clean %>%
  dplyr::filter(version == "gate_40")
#Hay una diferencia significativa entre la ubicación en la puerta 30 y la puerta 40 y para ver la tasa de retención, debemos esperar 7 días, ya que el jugador no llegará a la puerta 30 por jugar en un día. La recomendación para el negocio es que si queremos mantener la retención más alta, la puerta debe colocarse en el nivel 30 y no moverla al nivel 40.